
import { doLogin, getProfileAPI } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || '',
      userProfile: {}
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    clearUserInfo(state) {
      state.token = ''
      state.userProfile = {}
      removeToken()
    },
    setProfile(state, userProfile) {
      state.userProfile = userProfile
    }
  },
  actions: {
    async doLogin(ctx, { username, password }) {
      console.log('actions', username, password)

      const res = await doLogin({ username, password })
      ctx.commit('setToken', res.data.token)
    },
    async getUserProfile(ctx) {
      const res = await getProfileAPI()
      ctx.commit('setProfile', res.data)
      // 这里如果想要当前函数内的数据给到另外一个js模块使用
      // 直接把目标数据return出去
      return res.data.permissions
    }
  }
}
export const TOKEN_KEY = 'token'
